package com.aistar.controller;

import com.aistar.pojo.Customer;
import com.aistar.service.CustomerService;
import com.aistar.service.impl.CustomerServiceImpl;
import com.aistar.util.GetMessageCode;
import com.aistar.util.ServerResponse;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet(name = "customer",urlPatterns = "/customer")
public class CustomerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");

        switch (op){
            case "getbytelno": getByTelno(request,response);break;
            case "getcode": getCode(request,response);break;
            case "add": add(request,response);break;
            case "login": login(request,response);break;
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    //根据手机号查询用户
    private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收参数 (账号属于哪种)
        Integer type = Integer.valueOf(request.getParameter("type"));
        String account = request.getParameter("account");
        String custPassword = request.getParameter("custPassword");


        //2. service
        CustomerService service = new CustomerServiceImpl();
       ServerResponse serverResponse = service.getByAccountAndPwd(type,account,custPassword);

        if(serverResponse.getStatus() ==1){//登录成功
            request.getSession().setAttribute("loginCustomer",serverResponse.getData());
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(serverResponse));

    }




        //根据手机号查询用户
    private void getByTelno(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String telnoStr = request.getParameter("telno");
        long telno = Long.valueOf(telnoStr);

        CustomerService service = new CustomerServiceImpl();
        ServerResponse serverResponse = service.getByTelno(telno);
        System.out.println("server response:"+serverResponse);

        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(serverResponse));
    }

    private void getCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String telnoStr = request.getParameter("telno");
        if(telnoStr == null){
             ServerResponse serverResponse = ServerResponse.getDataFailed("请输入手机号"); //0 手机号为空
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().println(JSON.toJSONString(serverResponse));
        }else{
            Long telno = Long.valueOf(telnoStr);
            CustomerService service = new CustomerServiceImpl();
            ServerResponse serverResponse = service.getByTelno(telno);
             if(serverResponse.getStatus() == 1){//手机号已经注册过，不能再注册，即不调用短信接口
                 response.getWriter().println(JSON.toJSONString(serverResponse));  // 1 手机号已经注册过
             }else{
                //调用短信接口
               // String code =  GetMessageCode.getCode(telnoStr);
                 String code  = "666666";
                 System.out.println("code:"+code);
                 ServerResponse serverResponseCode = ServerResponse.getDataSuccess(code);
                 serverResponseCode.setStatus(2);



               response.getWriter().println(JSON.toJSONString(serverResponseCode));// 2 成功

             }


        }
    }


    //添加用户
    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 接收参数（手机号、密码）
       Long telno = Long.valueOf(request.getParameter("custTelno"));
       String custPassword = request.getParameter("custPassword");
        //2. 封装成Customer
        Customer customer = new Customer();
        customer.setCustTelno(telno);
        customer.setCustPassword(custPassword);
        customer.setCustRegistTime(new Date()); // 注册时间

        //3. service 添加
        CustomerService service = new CustomerServiceImpl();
        ServerResponse serverResponse = service.save(customer);

        //4. serverResponse json
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(serverResponse));
    }












    /*    //获得验证码
    private void getCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String telnoStr = request.getParameter("telno");
        long telno = Long.valueOf(telnoStr);

        PrintWriter out = response.getWriter();
        CustomerService service = new CustomerServiceImpl();
        Customer customer = service.getByTelno(telno);

        if(customer !=null){//有效账户
             String code = "666666";
          //  String code = GetMessageCode.getCode(telnoStr);
            out.print(code);
            System.out.println("用户已经存在，可以登录，已获得验证码：" + code);
        }else{ //无效账户，即账户尚未注册
            out.print(JSON.toJSONString("该手机号尚未注册，请先注册"));
            System.out.println("该手机号尚未注册，请先注册");
        }

    }
*/
  //用户登录
   /* private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1. 根据获取到的手机号发送验证码
        String telnoStr = request.getParameter("telno");
        Long telno = Long.valueOf(telnoStr);
        CustomerService service = new CustomerServiceImpl();
        Customer customer  = service.getByTelno(telno);
        System.out.println("登录的用户信息是:"+ customer);
        if(customer!=null){
            request.getSession().setAttribute("userName",customer.getCustName());
            request.getSession().setAttribute("userId",customer.getCustId());
            request.getSession().setAttribute("loginUser",customer);
        }
        request.getRequestDispatcher("/job?op=getlatest").forward(request,response);
    }*/

}
